package org.flexspeedup.blog.controllers
{
import mx.rpc.AsyncToken;
import mx.rpc.Responder;
import mx.rpc.remoting.RemoteObject;

import org.flexspeedup.blog.events.RemovePendingChangesEvent;
import org.flexspeedup.blog.events.ShowPopUpEvent;
import org.flexspeedup.blog.models.IStaticPage;
import org.flexspeedup.blog.models.PopUpModel;
import org.flexspeedup.blog.types.ServiceType;
import org.flexspeedup.utils.SwizDispatcherBase;

/**
 * @inheritDoc
 */
public class StaticPageController extends SwizDispatcherBase implements IStaticPageController
{

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new <code>StaticPageController</code> instance.
     */
    public function StaticPageController()
    {
    }

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  blogService
    //----------------------------------
    /**
     *  @private
     */    
    private var _blogService:RemoteObject;
    
    [Bindable]
    /**
     * blogService bean
     */
    public function get blogService():RemoteObject
    {
        return _blogService;
    }

    [Inject(source="blogService")]
    public function set blogService(value:RemoteObject):void
    {
        _blogService = value;
    }

    //----------------------------------
    //  staticPageModel
    //----------------------------------
    /**
     *  @private
     */    
    private var _staticPageModel:IStaticPage;
    
    [Bindable]
    /**
     * staticPageModel bean
     */
    public function get staticPageModel():IStaticPage
    {
        return _staticPageModel;
    }
    
    [Inject(source="staticPageModel")]
    public function set staticPageModel(value:IStaticPage):void
    {
        _staticPageModel = value;
    }
    
    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------    

    /**
     * @inheritDoc
     */    
    public function saveStaticPage(page:IStaticPage):void
    {
        var token:AsyncToken = 
            blogService.getOperation(ServiceType.SAVE_STATIC_PAGE).send(
                page, page.name);
            
        token.addResponder(new Responder(
            saveStaticPage_resultHandler,
            saveStaticPage_faultHandler
        ));
    }
    
    /**
     * @inheritDoc
     */    
    public function getStaticPage(pageName:String):void
    {
        var token:AsyncToken = 
            blogService.getOperation(ServiceType.GET_STATIC_PAGE).send(pageName);
            
        token.addResponder(new Responder(
            getStaticPage_resultHandler,
            getStaticPage_faultHandler
        ));
    }
    
    /**
     * @inheritDoc
     */    
    public function deleteStaticPage(pageName:String):void
    {
        trace ("delete page");
    }
    
    //----------------------------------
    //  populateModel(page:IStaticPage
    //----------------------------------
    /**
     * @private
     */
    private function populateModel(page:IStaticPage):void
    {
        if (!page)
            return;

        staticPageModel.title = page.title;
        staticPageModel.body = page.body;
        staticPageModel._key = page._key;
        staticPageModel.name = page.name;
    } 

    //--------------------------------------------------------------------------
    //
    //  event handlers
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  getStaticPage_resultHandler
    //----------------------------------
    /**
     * @private
     */
    private function getStaticPage_resultHandler(data:Object):void
    {
        populateModel(data.result);
    }    
    
    //----------------------------------
    //  getStaticPage_faultHandler
    //----------------------------------
    /**
     * @private
     */
    private function getStaticPage_faultHandler(info:Object):void
    {
        faultHandler(info);
    }

    //----------------------------------
    //  saveStaticPage_resultHandler
    //----------------------------------
    /**
     * @private
     */
    private function saveStaticPage_resultHandler(data:Object):void
    {
        populateModel(data.result);
        dispatchEvent(new RemovePendingChangesEvent());
    }
    
    //----------------------------------
    //  saveStaticPage_faultHandler
    //----------------------------------
    /**
     * @private
     */
    private function saveStaticPage_faultHandler(info:Object):void
    {
        faultHandler(info);
    }

    //----------------------------------
    //  _faultHandler
    //----------------------------------
    /**
     * @private
     */
    private function faultHandler(info:Object):void
    {
        var popUpModel:PopUpModel = new PopUpModel
        (
            "error",
            "there was an error processing your request, please try again"
        );
        dispatchEvent(new ShowPopUpEvent(popUpModel));        
    }
}
}